﻿// ===============================================================================
// RadioButton.cs
// EOS Web Framework
// ===============================================================================
// Copyright (c) EOS Development Group. All rights reserved.
// ===============================================================================

namespace EOS.Framework.Web.Mvc.Controls
{
    using System;
    using System.Globalization;

    /// <summary>
    /// Represents a default (x)html radio button.
    /// </summary>
    public class RadioButton : InputControl
    {
        #region Properties

        /// <summary>
        /// Indicates, if the radio button is checked.
        /// </summary>
        private bool isChecked;

        /// <summary>
        /// Gets or sets a value indicating whether this instance is checked.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is checked; otherwise, <c>false</c>.
        /// </value>
        public bool IsChecked
        {
            get { return isChecked; }
            set { isChecked = value; }
        }

        /// <summary>
        /// The value of the radio button.
        /// </summary>
        private object value;

        /// <summary>
        /// Gets or sets the value of the radio button.
        /// </summary>
        /// <value>The value.</value>
        public object Value
        {
            get { return value;}
            set { this.value = value;}
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="CheckBox"/> class.
        /// </summary>
        /// <param name="value">The value of the radio button. Cannot be null.</param>
        /// <exception cref="ArgumentNullException">
        /// 	<para><paramref name="markup"/> is null (Nothing in Visual Basic).</para>
        /// 	<para>- or -</para>
        /// 	<para><paramref name="value"/> is null (Nothing in Visual Basic).</para>
        /// </exception>
        public RadioButton(object value)
            : this(value, false)
        { 
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CheckBox"/> class with an value, indicating 
        /// if the radio button is checked.
        /// </summary>
        /// <param name="value">The value of the radio button. Cannot be null.</param>
        /// <param name="isChecked">if set to <c>true</c> the radio button is checked.</param>
        /// <exception cref="ArgumentNullException">
        /// 	<para><paramref name="markup"/> is null (Nothing in Visual Basic).</para>
        /// 	<para>- or -</para>
        /// 	<para><paramref name="value"/> is null (Nothing in Visual Basic).</para>
        /// </exception>
        public RadioButton(object value, bool isChecked)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            this.value = value;
            this.isChecked = isChecked;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CheckBox"/> class with an value, indicating
        /// if the radio button is checked and the name of the radio button.
        /// </summary>
        /// <param name="value">The value of the radio button. Cannot be null.</param>
        /// <param name="isChecked">if set to <c>true</c> the radio button is checked.</param>
        /// <param name="name">The name of the button.</param>
        /// <exception cref="ArgumentNullException">
        /// 	<para><paramref name="markup"/> is null (Nothing in Visual Basic).</para>
        /// 	<para>- or -</para>
        /// 	<para><paramref name="value"/> is null (Nothing in Visual Basic).</para>
        /// 	<para>- or -</para>
        /// 	<para><paramref name="name"/> is null (Nothing in Visual Basic).</para>
        /// </exception>
        /// <exception cref="ArgumentException"><paramref name="name"/> is a string of length 0 or 
        /// has only whitespaces.</exception>
        public RadioButton(object value, bool isChecked, string name)
            : base(name)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            this.value = value;
            this.isChecked = isChecked;
        }

        #region Control Members

        /// <summary>
        /// Called when the web control should be rendered.
        /// </summary>
        /// <param name="writer">The xhtml writer.</param>
        /// <param name="context">Context values for the page, where the control is embedded to.</param>
        /// <exception cref="ControlMarkupException">
        /// 	<para>The xml tag element the control is assigned to doesnt have the name input.</para>
        /// 	<para>- or -</para>
        /// 	<para>The xml tag element doesnt have an attribute type or the attribute doesnt have the value radio.</para>
        /// </exception>
        protected override void OnRender(IXhtmlWriter writer, PageContext context)
        {
            if (TagName.ToLower().Equals("input") == false)
            {
                throw new ControlMarkupException(
                    string.Format(CultureInfo.CurrentCulture, Properties.Resources.InvalidTagName, "input"));
            }

            if (!VerifyAttributeHasValue("type", "radio"))
            {
                throw new ControlMarkupException(
                    string.Format(CultureInfo.CurrentCulture, Properties.Resources.WrongTagAttributeValue, "type", "radio"));
            }

            WriteBeginAttachedTag(writer);
            
            writer.WriteAttribute("value", value.ToString());
            if (isChecked)
            {
                writer.WriteAttribute("checked", "checked");
            }

            WriteEndAttachedTag(writer);
        }

        #endregion
    }
}
